﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class CycleCountReleaseDA : ICycleCountReleaseDA
    {
        private string CC_CRITERIA_ID = "@CC_CRITERIA_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string RELEASE_DATE = "@RELEASE_DATE";
        private string RELEASE_ID = "@RELEASE_ID";
        private string SQL_CHECK_CYCLE_COUNT_RELEASE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@CYCLE_COUNT_RELEASE WHERE RELEASE_ID = @RELEASE_ID ";
        private string SQL_DELETE_CYCLE_COUNT_RELEASE = " DELETE FROM @_@CYCLE_COUNT_RELEASE WHERE RELEASE_ID = @RELEASE_ID ";
        private string SQL_INSERT_CYCLE_COUNT_RELEASE = " INSERT INTO @_@CYCLE_COUNT_RELEASE ( WH_ID, RELEASE_ID, RELEASE_DATE, CC_CRITERIA_ID, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,STATUS ) VALUES ( @WH_ID, @RELEASE_ID, @RELEASE_DATE, @CC_CRITERIA_ID, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE(),@STATUS )  ";
        private string SQL_SELECT_ALL_CYCLE_COUNT_RELEASE = " SELECT  ROWID, WH_ID, RELEASE_ID, RELEASE_DATE, CC_CRITERIA_ID, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,STATUS  FROM  @_@CYCLE_COUNT_RELEASE ";
        private string SQL_SELECT_ALL_CYCLE_COUNT_RELEASE_LANGUAGE = " SELECT  CCR.ROWID, CCR.WH_ID, CCR.RELEASE_ID, CCR.RELEASE_DATE, CCR.CC_CRITERIA_ID, CCR.CREATED_BY, CCR.CREATED_DATE, CCR.UPDATED_BY,  CCR.UPDATED_DATE, CCR.STATUS,T.[TEXT] AS STATUS_D FROM  @_@CYCLE_COUNT_RELEASE CCR LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = CCR.STATUS AND SCD.SYS_CODE_TYPE = 'CCRStatus' LEFT JOIN sysadm.TRANSLATION T ON 'CCRStatus.'+CCR.STATUS = T.ITEM_ID  AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY =@LANGUAGE";
        private string SQL_SELECT_BY_CYCLE_COUNT_QUERY_ENTITY = "SELECT DISTINCT  CCR.ROWID, CCR.WH_ID, CCR.RELEASE_ID, CCR.RELEASE_DATE, CCR.CC_CRITERIA_ID, CCR.CREATED_BY, CCR.CREATED_DATE, CCR.UPDATED_BY, CCR.UPDATED_DATE,CCR.STATUS FROM  @_@CYCLE_COUNT_RELEASE CCR INNER JOIN @_@CYCLE_COUNT_TAG CCT ON CCR.RELEASE_ID=CCT.RELEASE_ID";
        private string SQL_UPDATE_CYCLE_COUNT_RELEASE = " UPDATE @_@CYCLE_COUNT_RELEASE SET WH_ID = @WH_ID, RELEASE_DATE = @RELEASE_DATE, CC_CRITERIA_ID = @CC_CRITERIA_ID, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE(), STATUS=@STATUS  WHERE RELEASE_ID = @RELEASE_ID  ";
        private string SQL_UPDATE_CYCLE_COUNT_RELEASE_STATUS = " UPDATE @_@CYCLE_COUNT_RELEASE SET STATUS = @STATUS WHERE RELEASE_ID = @RELEASE_ID  ";
        private string STATUS = "@STATUS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckCycleCountReleaseIDUnique(string cycleCountReleaseID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RELEASE_ID", cycleCountReleaseID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_RELEASE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckCycleCountReleaseIDUnique(DataBase dataBase, DbTransaction tran, string cycleCountReleaseID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RELEASE_ID", cycleCountReleaseID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_RELEASE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteCycleCountRelease(List<string> cycleCountReleaseID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < cycleCountReleaseID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RELEASE_ID", cycleCountReleaseID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteCycleCountRelease(DataBase dataBase, DbTransaction tran, List<string> cycleCountReleaseID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < cycleCountReleaseID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RELEASE_ID", cycleCountReleaseID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllCycleCountRelease(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllCycleCountRelease(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public CycleCountReleaseInfo GetCycleCountReleaseByID(string cyclecountreleaseID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID) + " WHERE RELEASE_ID = @RELEASE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RELEASE_ID", cyclecountreleaseID) };
            CycleCountReleaseInfo cycleCountReleaseInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    cycleCountReleaseInfo = new CycleCountReleaseInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["RELEASE_ID"].ToString(), (reader["RELEASE_DATE"].ToString() != "") ? DateTime.Parse(reader["RELEASE_DATE"].ToString()) : (CS00003 = new DateTime()), reader["CC_CRITERIA_ID"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["STATUS"].ToString());
                }
            }
            return cycleCountReleaseInfo;
        }

        public CycleCountReleaseInfo GetCycleCountReleaseByID(DataBase dataBase, DbTransaction tran, string cyclecountreleaseID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID) + " WHERE RELEASE_ID = @RELEASE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RELEASE_ID", cyclecountreleaseID) };
            CycleCountReleaseInfo cycleCountReleaseInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                cycleCountReleaseInfo = new CycleCountReleaseInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["RELEASE_ID"].ToString(), (reader["RELEASE_DATE"].ToString() != "") ? DateTime.Parse(reader["RELEASE_DATE"].ToString()) : (CS00003 = new DateTime()), reader["CC_CRITERIA_ID"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["STATUS"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return cycleCountReleaseInfo;
        }

        public DataSet GetCycleCountReleaseByQueryList(List<string> sqlWhere, CycleCountReleaseQueryEntity cyclecountreleaseQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_CYCLE_COUNT_RELEASE_LANGUAGE.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cyclecountreleaseQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cyclecountreleaseQuery.CurrentPage, cyclecountreleaseQuery.PageSize, cyclecountreleaseQuery.SortField, cyclecountreleaseQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetCycleCountReleaseByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, CycleCountReleaseQueryEntity cyclecountreleaseQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_BY_CYCLE_COUNT_QUERY_ENTITY.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cyclecountreleaseQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cyclecountreleaseQuery.CurrentPage, cyclecountreleaseQuery.PageSize, cyclecountreleaseQuery.SortField, cyclecountreleaseQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            temp = PagingHelper.GetPagingSQL(temp, cyclecountreleaseQuery.CurrentPage, cyclecountreleaseQuery.PageSize, cyclecountreleaseQuery.SortField, cyclecountreleaseQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertCycleCountRelease(CycleCountReleaseInfo cycleCountReleaseInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                cycleCountReleaseInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CycleCountRelease_Parameters(cycleCountReleaseInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertCycleCountRelease(DataBase dataBase, DbTransaction tran, CycleCountReleaseInfo cycleCountReleaseInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                cycleCountReleaseInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CycleCountRelease_Parameters(cycleCountReleaseInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_CycleCountRelease_Parameters(CycleCountReleaseInfo cycleCountReleaseInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.RELEASE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.RELEASE_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.CC_CRITERIA_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.STATUS, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(cycleCountReleaseInfo.WhID))
            {
                paramArray[0].Value = cycleCountReleaseInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = cycleCountReleaseInfo.ReleaseID;
            paramArray[2].Value = cycleCountReleaseInfo.ReleaseDate;
            paramArray[3].Value = cycleCountReleaseInfo.CcCriteriaID;
            paramArray[4].Value = cycleCountReleaseInfo.CreatedBy;
            paramArray[5].Value = cycleCountReleaseInfo.UpdatedBy;
            paramArray[6].Value = cycleCountReleaseInfo.Status;
            return paramArray;
        }

        public int UpdateCycleCountRelease(CycleCountReleaseInfo cycleCountReleaseInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                cycleCountReleaseInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CycleCountRelease_Parameters(cycleCountReleaseInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateCycleCountRelease(DataBase dataBase, DbTransaction tran, CycleCountReleaseInfo cycleCountReleaseInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                cycleCountReleaseInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CycleCountRelease_Parameters(cycleCountReleaseInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_RELEASE.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateCycleCountReleaseStatus(DataBase dataBase, DbTransaction tran, string ReleaseID, string Status, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATUS", Status), new SqlParameter("@RELEASE_ID", ReleaseID) };
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_RELEASE_STATUS.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

